package leetcode.linkedlist;

import leetcode.linkedlist.common.LinkedListUtils;
import leetcode.linkedlist.common.ListNode;

class Solution23 {
    public ListNode mergeKLists(ListNode[] lists) {
//        return LinkedListUtils.mergeKLists(lists);
        // 使用优先队列维护最小值速度比每次扫描最小值快得多
        return LinkedListUtils.mergeKListsPro(lists);
    }
}

public class leetcode23 {
    public static void main(String[] args) {
        Solution23 solution23 = new Solution23();
        int[][] lists = {{1, 4, 5}, {1, 3, 4}, {2, 6}};
        int[][] lists1 = {{}};
        test23(solution23, lists);
        test23(solution23, lists1);
    }

    private static void test23(Solution23 solution23, int[][] lists) {
        int k = lists.length;
        ListNode[] headNodes = new ListNode[k];
        for (int i = 0; i < k; i++) {
            headNodes[i] = LinkedListUtils.generateLinkedListByArray(lists[i]);
        }
        ListNode head = solution23.mergeKLists(headNodes);
        System.out.println(LinkedListUtils.getLinkedListVals(head));
    }
}
